package com.cr.business.controller;

import com.cr.business.domain.dto.EntityReqDto;
import com.cr.business.domain.vo.EntityExportData;
import com.cr.business.domain.vo.EntityListData;
import com.cr.business.service.ITbUserEntityInfoService;
import com.cr.common.annotation.Log;
import com.cr.common.constant.Constants;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.core.page.TableDataInfo;
import com.cr.common.enums.BusinessType;
import com.cr.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实体信息控制器
 */
@RestController
@RequestMapping("/entity/info")
public class TbUserEntityInfoController extends BaseController {

    @Resource
    private ITbUserEntityInfoService tbUserEntityInfoService;

    /**
     * 查询主体信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:entity:list')")
    @GetMapping("/list")
    public TableDataInfo list(EntityReqDto dto)
    {
        dto.setReqType("info");
        startPage();
        List<EntityListData> entityListData = tbUserEntityInfoService.selectEntityList(dto);
        return getDataTable(entityListData);
    }

    /**
     * 删除主体信息
     */
    @PreAuthorize("@ss.hasPermi('business:entity:delete')")
    @Log(title = "主体管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{entityIds}")
    public AjaxResult remove(@PathVariable String[] entityIds)
    {
        return toAjax(tbUserEntityInfoService.deleteEntityInfoByIds(entityIds));
    }

    /**
     * 导出信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:entity:export')")
    @Log(title = "主体管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EntityReqDto dto)
    {
        List<EntityExportData> list = tbUserEntityInfoService.selectEntityExportList(dto);
        ExcelUtil<EntityExportData> util = new ExcelUtil<>(EntityExportData.class);
        util.exportExcel(response, list, "主体信息数据");
    }

    /**
     * 停产
     */
    @PreAuthorize("@ss.hasPermi('business:entity:stop')")
    @Log(title = "主体停产", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{entityId}")
    public AjaxResult stop(@PathVariable String entityId)
    {
        return toAjax(tbUserEntityInfoService.updateEntityInfoStatus(entityId, "1"));
    }

    /**
     * 注销
     */
    @PreAuthorize("@ss.hasPermi('business:entity:cancel')")
    @Log(title = "主体注销", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{entityId}")
    public AjaxResult cancel(@PathVariable String entityId)
    {
        return toAjax(tbUserEntityInfoService.updateEntityInfoStatus(entityId, "2"));
    }

    /**
     * 恢复
     */
    @PreAuthorize("@ss.hasAnyPermi('business:entity:stop"+ Constants.PERMISSION_DELIMETER +"business:entity:cancel')")
    @Log(title = "主体状态恢复正常", businessType = BusinessType.UPDATE)
    @PutMapping("/toNormal/{entityId}")
    public AjaxResult toNormal(@PathVariable String entityId)
    {
        return toAjax(tbUserEntityInfoService.updateEntityInfoStatus(entityId, "0"));
    }
}
